package com.sergivonavi.materialbanner.app.slice;

import com.sergivonavi.materialbanner.Banner;
import com.sergivonavi.materialbanner.app.ResourceTable;
import com.sergivonavi.materialbanner.app.adapter.ItemsAdapter;
import com.sergivonavi.materialbanner.app.snackbar.SnackBar;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FromLayoutAbilitySlice extends AbilitySlice {
    private Banner             mBanner;
    private Button             mBtnBanner;
    private DirectionalLayout mDlFrom;
    private DirectionalLayout mFromBanner;
    private MyAdapter mAdapter;

    @Override
    protected void onStart(Intent intent) {
        try {
            int color = getResourceManager().getElement(ResourceTable.Color_title_bar_bg).getColor();
            getWindow().setStatusBarColor(color);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_sample_ability_from_layout);

        Component from = findComponentById(ResourceTable.Id_dl_from);
        if (from instanceof DirectionalLayout) {
            mDlFrom = (DirectionalLayout) from;
        }
        Component fromBanner = LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_from_banner, null, false);
        if (fromBanner instanceof DirectionalLayout) {
            mFromBanner = (DirectionalLayout) fromBanner;
        }
        Component btBanner = findComponentById(ResourceTable.Id_btn_banner);
        if (btBanner instanceof Button) {
            mBtnBanner = (Button) btBanner;
        }
        Component idBanner = mFromBanner.findComponentById(ResourceTable.Id_banner);
        if (idBanner instanceof Banner) {
            mBanner = (Banner) idBanner;
        }
        Component list = findComponentById(ResourceTable.Id_from_layout_list);
        if (list instanceof ListContainer) {
            ListContainer listContainer = (ListContainer) list;
            List<Integer> data = initData();
            mAdapter = new MyAdapter(this, data, ResourceTable.Layout_item_list);
            listContainer.setItemProvider(mAdapter);
        }
        initListener();
    }

    private void initListener() {
        mBtnBanner.setClickedListener(component -> {
            if (mBanner.getVisibility() == Component.HIDE) {
                mBanner.show();
                mAdapter.addHeaderView(mFromBanner);
            } else {
                mBanner.dismiss();
                mAdapter.removeHeaderView();
            }
        });

        mBanner.setLeftButtonListener(banner -> showUndoSnackBar("Banner: Left Button"));

        mBanner.setRightButtonListener(banner -> showUndoSnackBar("Banner: Right Button"));

        mBanner.setOnShowListener(() -> showUndoSnackBar("Banner: onShow"));

        mBanner.setOnDismissListener(() -> showUndoSnackBar("Banner: onDismiss"));
    }

    private List<Integer> initData() {
        ArrayList<Integer> data = new ArrayList<>();
        data.add(ResourceTable.Media_1);
        data.add(ResourceTable.Media_2);
        data.add(ResourceTable.Media_3);
        data.add(ResourceTable.Media_4);
        data.add(ResourceTable.Media_5);
        data.add(ResourceTable.Media_6);
        data.add(ResourceTable.Media_7);
        data.add(ResourceTable.Media_8);
        data.add(ResourceTable.Media_9);
        data.add(ResourceTable.Media_10);
        data.add(ResourceTable.Media_1);
        data.add(ResourceTable.Media_2);
        data.add(ResourceTable.Media_3);
        data.add(ResourceTable.Media_4);
        data.add(ResourceTable.Media_5);
        data.add(ResourceTable.Media_6);
        data.add(ResourceTable.Media_7);
        data.add(ResourceTable.Media_8);
        return data;
    }

    private void showUndoSnackBar(final String text) {
        new SnackBar.Builder(getAbility(), mDlFrom)

                // 弹框文本信息
                .withMessage(text)

                // 弹框文本信息颜色设置
                .withStyle(Color.getIntColor("#20c7c6"))

                // 弹框按钮颜色设置
                .withBackgroundColorId(Color.getIntColor("#000000"))
                .withTypeFace(Font.DEFAULT)
                .withClearQueued()

                // 弹框持续时间设置
                .withDuration(SnackBar.SHORT_SNACK)
                .show();
    }

    private class MyAdapter extends ItemsAdapter {
        public MyAdapter(Context context, List data, int layoutId) {
            super(context, data, layoutId);
        }

        @Override
        public void convert(GridViewHolder holder, int[] positions, List models) {
            for (int i = 0; i < positions.length; i++) {
                switch (i) {
                    case 0:
                        Image image1 = holder.getImageView(ResourceTable.Id_item_image1);
                        try {
                            Integer model = null;
                            Object obj = models.get(i);
                            if (obj instanceof Integer) {
                                model = (Integer) obj;
                                PixelMapElement pixelMapElement = new PixelMapElement(
                                        getContext().getResourceManager().getResource(model));
                                image1.setImageElement(pixelMapElement);
                            }
                        } catch (IOException | NotExistException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 1:
                        Image image2 = holder.getImageView(ResourceTable.Id_item_image2);
                        try {
                            Integer model = null;
                            Object obj = models.get(i);
                            if (obj instanceof Integer) {
                                model = (Integer) obj;
                                PixelMapElement pixelMapElement = new PixelMapElement(
                                        getContext().getResourceManager().getResource(model));
                                image2.setImageElement(pixelMapElement);
                            }
                        } catch (IOException | NotExistException e) {
                            e.printStackTrace();
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        @Override
        public void addHeaderView(Component header) {
            super.addHeaderView(header);
        }
    }
}
